//
//  XFDNetManager.m
//  XFDNetManager_Example
//
//  Created by 朱国良 on 2017/11/7.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#define CustomErrorDomain @"享飞DMS错误:"

#import "NSDictionary+HBSafely.h"
#import "XFDNetManager.h"
#import "HBMacros.H"
#import "FLToast+UIView.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "XFDUserManage.h"
#import "XFDNetAction.h"
@implementation XFDNetManager

+ (void)netManagerRequestWithMethod:(XMHTTPMethodType)method
                            WithAPI:(NSString *)api
                         WithParams:(NSDictionary*)params
                   WithSuccessBlock:(requestSuccessBlock)success
                    WithFailurBlock:(requestFailureBlock)failure withHUB:(BOOL)isHas {
    [XFDNetManager requestWithMethod:method WithAPI:api WithParams:params WithSuccessBlock:success WithFailurBlock:failure withHUB:isHas withHasToken:NO];
}
+ (void)netTokenManagerRequestWithMethod:(XMHTTPMethodType)method
                            WithAPI:(NSString *)api
                         WithParams:(NSDictionary*)params
                   WithSuccessBlock:(requestSuccessBlock)success
                    WithFailurBlock:(requestFailureBlock)failure withHUB:(BOOL)isHas{
    [XFDNetManager requestWithMethod:method WithAPI:api WithParams:params WithSuccessBlock:success WithFailurBlock:failure withHUB:isHas withHasToken:YES];
}

+ (void)requestWithMethod:(XMHTTPMethodType)method
              WithAPI:(NSString *)api
           WithParams:(NSDictionary*)params
     WithSuccessBlock:(requestSuccessBlock)success
          WithFailurBlock:(requestFailureBlock)failure withHUB:(BOOL)isHas withHasToken:(BOOL )isToken{
    if (isHas) {
        [MBProgressHUD showHUDAddedTo:THEAPP.window animated:YES];
    }
    [[XMCenter defaultCenter] sendRequest:^(XMRequest * _Nonnull request) {
        request.httpMethod = method;
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        if(isToken){
            [dic setObjectSafely:[XFDUserManage accessToken] forKey:@"X-TOKEN"];
            request.headers = dic;
        }
        request.api = api;
        request.requestSerializerType = kXMRequestSerializerJSON;
        request.parameters = params;
        request.timeoutInterval = 20;
        DLOG(@"request-----%@/n%@/n%@",request.url,api,params);
    } onSuccess:^(id  _Nullable responseObject) {
        if (isHas) {
            [MBProgressHUD hideHUDForView:THEAPP.window animated:YES];
        }
        DLOG(@"URLResponseObject-----%@",responseObject);
        if (responseObject) {
            if ([responseObject[@"status"] isEqualToString:@"success"]) {
                if (success) {
                    success(responseObject[@"data"]);
                }
            }else if ([responseObject[@"status"] isEqualToString:@"invalidToken"]){
                if (failure) {
                    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[responseObject objectForKeySafely:@"message"]                                                                      forKey:NSLocalizedDescriptionKey];
                    [[XFDNetAction share]no_token];
                    NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:1000 userInfo:userInfo];
                    NSString *tip = [responseObject objectForKeySafely:@"message"];
                    [THEAPP.window makeToast:@"您的登录信息已经过期，请重新登录！" duration:2];
                    failure(aError,tip);
                }
            }else if ([responseObject[@"status"] isEqualToString:@"expiredToken"]){
                if (failure) {
                    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[responseObject objectForKeySafely:@"message"]                                                                      forKey:NSLocalizedDescriptionKey];
                    [[XFDNetAction share]no_token];
                    NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:1000 userInfo:userInfo];
                    NSString *tip = [responseObject objectForKeySafely:@"message"];
                    failure(aError,tip);
                    [THEAPP.window makeToast:@"您的登录信息已经过期，请重新登录！" duration:2];
                }
                
            }else if ([responseObject[@"status"] isEqualToString:@"invalidRole"]){
                if (failure) {
                    [[XFDNetAction share]invalidRole:[responseObject objectForKeySafely:@"message"]];
                }
            }else {
                if (failure) {
                    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[responseObject objectForKeySafely:@"message"]                                                                      forKey:NSLocalizedDescriptionKey];
                    NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:1000 userInfo:userInfo];
                    NSString *tip = [responseObject objectForKeySafely:@"message"];
                    failure(aError,tip);
                    [THEAPP.window makeToast:tip duration:2];
                    DLOG(@"URLResponseObject-----%@",aError);
                }
            }
        }else{
            NSString *tip  = @"网络出错，请稍后再试";
            [THEAPP.window makeToast:tip duration:2];
            NSDictionary *userInfo = [NSDictionary dictionaryWithObject:tip                                                                      forKey:NSLocalizedDescriptionKey];
            NSError *aError = [NSError errorWithDomain:CustomErrorDomain code:1000 userInfo:userInfo];
            
            failure(aError,tip);
            DLOG(@"URLResponseObject-----%@",aError);
        }
    } onFailure:^(NSError * _Nullable error) {
        DLOG(@"URLResponseObject-----%@",error);
        if (failure) {
            if (isHas) {
                [MBProgressHUD hideHUDForView:THEAPP.window animated:YES];
            }
            NSString *tip  = @"网络出错，请稍后再试";
            [THEAPP.window makeToast:tip duration:2];
            failure(error,tip);
        }
    }];

}

@end
